<?php

class Model_Map {

    public function insertMap($map) {
        if (isset($map) && $map != null) {
            $link = $map['mapLink'];
            $query = sprintf("INSERT INTO `mapimage`(`link`) 
            VALUES ('%s')"
                    , mysql_real_escape_string($link));
            $db = Model_Connections::getInstance()->getDbConnect();
            $data = $db->query($query);
            $query = "Select Max(id) from mapimage";
            $id = $db->fetchAll($query);
            if ($id != null)
                return $id[0]['Max(id)'];
            return null;
        }
    }

    public function api_getMapById($id) {
        if ($id != null) {
            $id = mysql_real_escape_string($id);

            $sql = "Select link from mapimage where id = " . $id;

            $db = Model_Connections::getInstance()->getDbConnect();
            $data = $db->fetchAll($sql);
            return $data;
        }
        return null;
    }

    public function updateMapById($id, $mapLink) {
        if (isset($id) && $id != null && isset($mapLink) && $mapLink != null) {
            $id = mysql_real_escape_string($id);
            $mapLink = mysql_real_escape_string($mapLink);
            $sql = "UPDATE `mapimage` SET `link` = N'" . $mapLink . "'  WHERE id = '" . $id . "'";
            $db = Model_Connections::getInstance()->getDbConnect();
            $data = $db->query($sql);
            return $data;
        }
        return null;
    }

    public function getMapImageAllById($id) {
        $sql = "SELECT * FROM mapimage where id = " . $id;
        $db = Model_Connections::getInstance()->getDbConnect();
        $data = $db->fetchAll($sql);
        return $data;
    }

    public function deleteMap($id) {
        if (isset($id) && $id != null) {
            $sql = sprintf("DELETE FROM `mapimage` WHERE id = '%d' ", mysql_real_escape_string($id));
            $db = Model_Connections::getInstance()->getDbConnect();
            $data = $db->query($sql);
            return $data;
        }
    }

}

?>
